package main

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/activity"
	ServiceActivity "gq_volunteer_platform/service/activity"
	"time"
)

func ActionFinishActivity() error {

	var actList []activity.Activity

	var timeNowStr = time.Now().Format(time.DateTime)

	// 查询 进行中 应当结束的 活动
	if err := bean.MysqlDb.Model(&activity.Activity{}).
		Where("`status`=?", "进行中").
		Where("`end_time`<=?", timeNowStr).
		Find(&actList).Error; err != nil {

		return err
	}

	// 查询进行中的活动
	for _, act := range actList {
		if err := ServiceActivity.ActivityFinish(act); err != nil {
			return err
		}
	}

	return nil
}

func ActionStartActivity() error {

	var actList []activity.Activity

	var timeNowStr = time.Now().Format(time.DateTime)

	// 查询 招募中 应当开始的 活动
	if err := bean.MysqlDb.Model(&activity.Activity{}).
		Where("`status`=?", "招募中").
		Where("`start_time`<=?", timeNowStr).
		Find(&actList).Error; err != nil {

		return err
	}

	// 查询进行中的活动
	for _, act := range actList {
		if err := ServiceActivity.ActivityStart(act); err != nil {
			return err
		}
	}

	return nil
}
